package com.kawa.kemanage.controller;

import com.kawa.kemanage.entity.NewsDraft;
import com.kawa.kemanage.service.NewsDraftService;
import com.kawa.kemanage.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻草稿控制器
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping({"/api/news/draft", "/app/news/draft"})
@Tag(name = "新闻草稿管理", description = "新闻草稿的增删改查等操作")
public class NewsDraftController {
    
    @Autowired
    private NewsDraftService newsDraftService;
    
    /**
     * 保存草稿
     */
    @Operation(summary = "保存草稿", description = "保存或更新新闻草稿")
    @PostMapping("/save")
    public Result saveDraft(@RequestBody NewsDraft draft) {
        try {
            NewsDraft savedDraft = newsDraftService.saveDraft(draft);
            return savedDraft != null ? Result.success(savedDraft) : Result.error("草稿保存失败");
        } catch (Exception e) {
            log.error("保存草稿失败", e);
            return Result.error("保存草稿失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取草稿列表
     */
    @Operation(summary = "获取草稿列表", description = "分页获取用户的草稿列表")
    @GetMapping("/list")
    public Result getDraftList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") Integer size,
            @Parameter(description = "作者ID") @RequestParam Integer authorId) {
        try {
            PageInfo<NewsDraft> pageInfo = newsDraftService.getDraftList(authorId, page, size);
            return Result.success(pageInfo);
        } catch (Exception e) {
            log.error("获取草稿列表失败", e);
            return Result.error("获取草稿列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取草稿详情
     */
    @Operation(summary = "获取草稿详情", description = "根据ID获取草稿详情")
    @GetMapping("/{id}")
    public Result getDraftById(@Parameter(description = "草稿ID") @PathVariable Integer id) {
        try {
            NewsDraft draft = newsDraftService.getDraftById(id);
            return draft != null ? Result.success(draft) : Result.error("草稿不存在");
        } catch (Exception e) {
            log.error("获取草稿详情失败", e);
            return Result.error("获取草稿详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除草稿
     */
    @Operation(summary = "删除草稿", description = "根据ID删除草稿")
    @DeleteMapping("/{id}")
    public Result deleteDraft(@Parameter(description = "草稿ID") @PathVariable Integer id) {
        try {
            boolean result = newsDraftService.deleteDraft(id);
            return result ? Result.success("草稿删除成功") : Result.error("草稿删除失败");
        } catch (Exception e) {
            log.error("删除草稿失败", e);
            return Result.error("删除草稿失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除草稿
     */
    @Operation(summary = "批量删除草稿", description = "批量删除多个草稿")
    @DeleteMapping("/batch")
    public Result batchDeleteDrafts(@RequestBody List<Integer> ids) {
        try {
            boolean result = newsDraftService.batchDeleteDrafts(ids);
            return result ? Result.success("批量删除成功") : Result.error("批量删除失败");
        } catch (Exception e) {
            log.error("批量删除草稿失败", e);
            return Result.error("批量删除草稿失败：" + e.getMessage());
        }
    }
} 